package com.neusoft.a132wxyorderingdish.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.fragments.CollectFragment;
import com.neusoft.a132wxyorderingdish.fragments.MyFragment;
import com.neusoft.a132wxyorderingdish.fragments.SearchFragment;
import com.neusoft.a132wxyorderingdish.fragments.ShopFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private LinearLayout mTabhome;
    private LinearLayout mTabcollect;
    private LinearLayout mTabsearch;
    private LinearLayout mTabmy;

    private ImageButton mImghome;
    private ImageButton mImgcollect;
    private ImageButton mImgsearch;
    private ImageButton mImgmy;

    private Fragment mfragment_home;
    private Fragment mfragment_collect;
    private Fragment mfragment_search;
    private Fragment mfragment_my;

    private TextView home;
    private TextView collect;
    private TextView search;
    private TextView my;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);
    }
    private void initEvent(){
        mTabhome.setOnClickListener(this);
        mTabcollect.setOnClickListener(this);
        mTabsearch.setOnClickListener(this);
        mTabmy.setOnClickListener(this);
    }
    private void initView(){
        mTabhome = (LinearLayout)findViewById(R.id.id_tab_home);
        mTabcollect = (LinearLayout)findViewById(R.id.id_tab_collect);
        mTabsearch = (LinearLayout)findViewById(R.id.id_tab_search);
        mTabmy = (LinearLayout)findViewById(R.id.id_tab_my);

        mImghome=(ImageButton)findViewById(R.id.id_tab_home_img);
        mImgcollect=(ImageButton)findViewById(R.id.id_tab_collect_img);
        mImgsearch=(ImageButton)findViewById(R.id.id_tab_search_img);
        mImgmy=(ImageButton)findViewById(R.id.id_tab_my_img);

        home=(TextView)findViewById(R.id.home);
        collect=(TextView)findViewById(R.id.collect);
        search=(TextView)findViewById(R.id.search);
        my=(TextView)findViewById(R.id.my);
    }
    private void setSelect(int i){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(mfragment_home == null){
                    mfragment_home = new ShopFragment();
                    transaction.add(R.id.id_content,mfragment_home);
                }else{
                    transaction.show(mfragment_home);
                }
                mImghome.setImageResource(R.drawable.ic_home);
                home.setTextColor(Color.parseColor("#B0C4DE"));
                break;

            case 1:
                if(mfragment_collect == null){
                    mfragment_collect = new CollectFragment();
                    transaction.add(R.id.id_content,mfragment_collect);
                }else{
                    transaction.show(mfragment_collect);
                }
                mImgcollect.setImageResource(R.drawable.ic_star);
                collect.setTextColor(Color.parseColor("#B0C4DE"));
                break;

            case 2:
                if(mfragment_search == null){
                    mfragment_search = new SearchFragment();
                    transaction.add(R.id.id_content,mfragment_search);
                }else{
                    transaction.show(mfragment_search);
                }
                mImgsearch.setImageResource(R.drawable.ic_pageview);
                search.setTextColor(Color.parseColor("#B0C4DE"));
                break;

            case 3:
                if(mfragment_my == null){
                    mfragment_my = new MyFragment();
                    transaction.add(R.id.id_content,mfragment_my);
                }else{
                    transaction.show(mfragment_my);
                }
                mImgmy.setImageResource(R.drawable.ic_person);
                my.setTextColor(Color.parseColor("#B0C4DE"));
                break;
        }
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){
        if(mfragment_home != null){
            transaction.hide(mfragment_home);
            home.setTextColor(Color.parseColor("#8a8a8a"));
        }

        if(mfragment_collect != null){
            transaction.hide(mfragment_collect);
            collect.setTextColor(Color.parseColor("#8a8a8a"));
        }

        if(mfragment_search != null){
            transaction.hide(mfragment_search);
            search.setTextColor(Color.parseColor("#8a8a8a"));
        }

        if(mfragment_my != null){
            transaction.hide(mfragment_my);
            my.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }
    @Override
    public void onClick(View v){
        resetImgs();
        switch (v.getId()){
            case R.id.id_tab_home:
                setSelect(0);
                break;

            case R.id.id_tab_collect:
                setSelect(1);
                break;

            case R.id.id_tab_search:
                setSelect(2);
                break;

            case R.id.id_tab_my:
                setSelect(3);
                break;
            default:
                break;
        }
    }
    @Override
    protected void  onResume(){
        super.onResume();
    }
    private void resetImgs(){
        mImghome.setImageResource(R.drawable.ic_home);
        mImgcollect.setImageResource(R.drawable.ic_star);
        mImgsearch.setImageResource(R.drawable.ic_pageview);
        mImgmy.setImageResource(R.drawable.ic_person);

    }
}






















